package com.hnhs.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hnhs.dao.CategoryDao;
import com.hnhs.info.PageInfo;
import com.hnhs.model.Category;
import com.hnhs.model.CategoryQuery;
import com.hnhs.threadLocal.LocalPageInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryService {

    @Resource
    private CategoryDao categoryDao;

    public List<Category> select(CategoryQuery query){
        if(query.getPageNum()!=null && query.getPageSize()!=null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }

        List<Category> categoryList = categoryDao.select(query);
        if(categoryList instanceof Page){
            Page page =(Page) categoryList;
            long total = page.getTotal();
            LocalPageInfoUtil.set(PageInfo.builder().Total(total).build());
        }

        return categoryList;
    }
    public Integer insert(Category category){
        return categoryDao.insert(category);
    }
    public Integer update(Category category){
        return  categoryDao.update(category);
    }
    public Integer updateStatus(Integer id){
        return categoryDao.updateStatus(id,0);
    }
}
